/**
 * 是否为移动端
 */
export const isMobile = () => {
  // @ts-ignore
  const userAgent = navigator.userAgent || navigator.vendor || window.opera
  return /android|avantgo|blackberry|iemobile|ipad|iphone|ipod|opera mini|opera mobi|palm|pocket|psp|series(4|6)0|symbian|windows ce|windows phone|xda|xiino/i.test(
    userAgent
  )
}
/**
 * object转string拼接
 */
export const objectToQueryStr = (obj: any = {}) => {
  return `?${Object.keys(obj)
    .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
    .join('&')}`
}
/**
 * url路径转object
 */
export const urlParamsToObject = (url: string) => {
  const obj = {} as any
  const params = url.slice(url.indexOf('?') + 1).split('&')
  for (let i = 0; i < params.length; i++) {
    const keyValue = params[i].split('=')
    const key = decodeURIComponent(keyValue[0])
    const value = decodeURIComponent(keyValue[1] || '')
    if (key === '') {
      continue
    }
    if (obj.hasOwnProperty(key)) {
      if (Array.isArray(obj[key])) {
        obj[key].push(value)
      } else {
        obj[key] = [obj[key], value]
      }
    } else {
      obj[key] = value
    }
  }
  return obj
}
/**
 * 解析blob数据
 */
export const parseBlobData = (blob: any) => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = function () {
      resolve(reader.result)
    }
    reader.onerror = function () {
      reject(reader.error)
    }
    reader.readAsText(blob)
  })
}
